package com.example.myapplication6;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        RecyclerView chatList = findViewById(R.id.list_chat_msg);
        EditText chatEdit = findViewById(R.id.edit_msg_send);
        Button chatSendBtn = findViewById(R.id.btn_msg_send);

        chatList.setLayoutManager(new LinearLayoutManager(this));

        List<ChatMsgModel> dataList = new ArrayList<>();

        ChatMsgAdapter adapter = new ChatMsgAdapter(this);
        adapter.setDataList(dataList);
        chatList.setAdapter(adapter);

        chatSendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = chatEdit.getText().toString();

                if (TextUtils.isEmpty(content))
                    return;

                ChatMsgModel model = new ChatMsgModel();
                model.setMsgTextContent(content);
                model.setSendOrRecv(true);

                ChatMsgModel autoRecv = new ChatMsgModel();
                autoRecv.setMsgTextContent("您好，我当前正忙，稍后回复您！");
                autoRecv.setSendOrRecv(false);

                dataList.add(model);
                dataList.add(autoRecv);

                //推荐使用局部刷新
                adapter.notifyDataSetChanged();
            }
        });
    }
}